package com.bwie.parkdemo.mvvm.viewmodel;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.view.View;

import com.bwie.lib_core.base.BaseEntity;
import com.bwie.lib_core.mvvm.base.BaseOBViewModel;
import com.bwie.parkdemo.mvvm.model.SelAllApplyModel;
import com.bwie.parkdemo.mvvm.model.entity.LoginEntity;
import com.bwie.parkdemo.mvvm.model.entity.SelAllApplyEntity;
import com.bwie.parkdemo.mvvm.model.entity.SelVisitorValuesEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 查询全部申请列表viewmodel
 * */
public class SelAllApplyViewModel extends BaseOBViewModel<SelAllApplyModel> {

    private LoginEntity.Values userValues;
    public SelAllApplyItemClickListener clickListener;
    public ObservableField<List<Object>>datas;
    public int type = 11;

    public SelAllApplyViewModel(@NonNull Application application) {
        super(application);
        clickListener = new SelAllApplyItemClickListener();
        datas = new ObservableField<>();
    }

    @Override
    protected SelAllApplyModel createModel() {
        return new SelAllApplyModel();
    }

    @Override
    protected void initData() {
        userValues = m.requestUser(getApplication());
        //封装网络请求接口的参数->由于接口中使用了sign
        //不同排列顺序加密结果不一样
        //time->如何取如何处理
        //{"userId":0,"time":"1584603015","sign":"32db886cb4c612dbd98e383d47d5d8a4"}
        String jsonStr = "{\"userId\":47,\"time\":\""+(System.currentTimeMillis()/1000)+"\"}";
        RequestBody body = RequestBody.create(MediaType.parse(mediaType),jsonStr);
        m.request(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    public void onNext(BaseEntity entity) {
        if (entity instanceof SelAllApplyEntity){
            SelAllApplyEntity selAllApplyEntity = (SelAllApplyEntity) entity;
            datas.set(selAllApplyEntity.getDatas());
        }else if (entity instanceof SelVisitorValuesEntity){

        }
    }

    //当前界面对应的列表的监听
    public class SelAllApplyItemClickListener implements BaseQuickAdapter.OnItemClickListener {

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            SelAllApplyEntity.Values entity = (SelAllApplyEntity.Values) adapter.getItem(position);
            //根据type值判断对应的申请类型->
            //使用对应的申请id以及用户id查询对应详情->
            //查询完毕->跳转对应详情页
            //{{"vistorId":66,"userId":1,"time":"1584607425","sign":"39abfbd03c6da36b2a9de67d64564423"}
            //                                                       39abfbd03c6da36b2a9de67d64564423
            //  "sign": "string",
            //  "time": "string",
            //  "userId": 0,
            //  "vistorId": 0
            //}
            if (entity.type == 0){
                //获取访客申请详情数据
                String jsonStr = "{\"vistorId\":"+entity.visitorId+",\"userId\":47," +
                        "\"time\":\""+(System.currentTimeMillis()/1000)+"\"}";
                RequestBody body = RequestBody.create(MediaType.parse(mediaType),jsonStr);
                m.requestVistorValues(body).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(SelAllApplyViewModel.this);
            }
        }
    }

}
